package com.app.piemoment.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.app.piemoment.R;
import com.app.piemoment.callback.PopoButtoCallBack;
import com.app.piemoment.util.Utils;

public class PopupButton implements OnClickListener {
	Button parent;
	PopupWindow pupup;
	RadioGroup rg;
	LinearLayout layout;
	String[] ss;
	int[] location;
	LayoutParams params;
	Context context;
	int color;
	Drawable drawable;
	String current;
	PopoButtoCallBack callback;

	public PopupButton(Context context, Button parent, String[] ss, PopoButtoCallBack callBack) {
		this.parent = parent;
		this.context = context;
		this.ss = ss;
		parent.setOnClickListener(this);
		color = Color.WHITE;
		this.callback = callBack;
		if (pupup == null) {
			initPop();
		}
	}

	public void initPop() {
		layout = new LinearLayout(context);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setBackgroundColor(Color.parseColor("#c94074"));
		layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		float testSize = 16.f;
		for (int i = 0; i < ss.length; i++) {
			Button c = new Button(context);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT);
			lp.setMargins(0, 0, 5, 0);
			c.setLayoutParams((lp));
			c.setTextColor(color);
			c.setTextSize(testSize);
			c.setText(ss[i]);
			c.setId(i);
			c.setBackgroundColor(Color.parseColor("#ff5898"));
			c.setGravity(Gravity.CENTER_VERTICAL);
			c.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					current = ((TextView) v).getText().toString();
					if(callback!=null) {
						callback.getCurrentValue(current);
					}
					dismissPop();
				}
			});
			layout.addView(c);
		}
		pupup = new PopupWindow(layout, Utils.dipToPixels(context, 60),
				LayoutParams.WRAP_CONTENT);
		pupup.getContentView().setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				dismissPop();
			}
		});
	}
	
	public String getCurrentText() {
		return current;
	}

	public void showPopWindow() {
		pupup.showAsDropDown(parent);
		pupup.setFocusable(true);
		pupup.setTouchable(true);
		parent.setTextColor(Color.WHITE);
		parent.invalidate();
		pupup.update();
	}

	public void dismissPop() {
		if (pupup != null) {
			pupup.dismiss();

			parent.setTextColor(color);
			parent.invalidate();
		}
	}

	public void onClick(View v) {
		if (pupup != null && pupup.isShowing()) {
			dismissPop();
		} else {
			showPopWindow();
		}
	}

	public boolean isShowing() {
		return pupup.isShowing();
	}
}

